﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class contest
    {
        public static dynamic rejudgeStatus(int rid)
        {
            var sql = new Sql.SqlDataContext();
            var s = sql.ContestStatus.FirstOrDefault(row => row.rid == rid);
            if (s == null) return new
            {
                success = false,
                msg = "状态不存在"
            };
            var con = sql.Contest.FirstOrDefault(row => row.cid == s.cid);
            if(con == null)
                return new{
                    success = false,
                    msg = "比赛不存在"
                };
            if (s.status >= 3 && !s.god)
            {
                // Update PS
                var ps = sql.ContestProblemStatistics.FirstOrDefault(row => row.ps == s.ps && row.cid == s.cid);
                if (ps != null)
                {
                    --ps.submit;
                    if (s.status == 3) --ps.accept;
                }

                // Update Solve
                var so = sql.ContestSolve.FirstOrDefault(row => row.username == s.username && row.ps == s.ps && row.cid == s.cid);
                if (so != null)
                {
                    --so.submit;
                    if (s.status == 3)
                    {
                        --so.ac;
                        if (so.bestRid == s.rid)
                        {
                            var newBest = (from s2 in sql.ContestStatus
                                           where s2.username == s.username && s2.cid == s.cid && s2.ps == s.ps && s2.status == 3 && s2.rid != s.rid && !s2.god
                                           orderby s2.exeTime, s2.exeMem, s2.code.Length, s2.submitTime
                                           select s2).FirstOrDefault();
                            so.bestRid = newBest == null ? null : (int?)newBest.rid;
                        }
                    }
                }

                // Update Ranklist
                var rank = sql.Ranklist.FirstOrDefault(row => row.cid == s.cid && row.username == s.username);
                
                bool pass = sql.ContestStatus.Any(  row => row.cid == s.cid && row.username == s.username &&
                                                    row.ps == s.ps && row.status == 3);
                --rank.submit;
                if(s.status == 3)
                --rank.ac;
                int notpass = sql.ContestSolve.FirstOrDefault(row => row.cid == s.cid && row.username == s.username && row.ps == s.ps).submit;
                if (!pass && s.status == 3)
                {
                    rank.usetime -= notpass * 20 + (int)(s.submitTime - con.startTime).TotalMinutes;
                }

            }

            // Reset Status
            s.status = 0;
            s.exeTime = s.exeMem = null;
            s.ceInfo = s.exeOutput = s.specialOutput = null;
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
